package com.sikao.app.adapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.sikao.app.R;
import com.sikao.app.myinterface.Callback;
import com.sikao.app.net.HttpApi;

import static com.sikao.app.R.id.iv1;

public class GvAddStepPricesTypeProfilePicItemAdapter extends BaseAdapter {
    private Callback picCallBack;
    private List<String> objects = new ArrayList<String>();
    private ImageLoader imageLoader = ImageLoader.getInstance();
    private Context context;
    private LayoutInflater layoutInflater;

    public GvAddStepPricesTypeProfilePicItemAdapter(Context context,List<String> objects , Callback picCallBack) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
        this.objects=objects;
        this.picCallBack=picCallBack;
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public String getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       // if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.gv_add_step_prices_type_profile_pic_item, null);
            convertView.setTag(new ViewHolder(convertView));
        //}
        initializeViews(getItem(position), (ViewHolder) convertView.getTag(),position);
        return convertView;
    }

    private void initializeViews(String object, ViewHolder holder, final int position) {
        //TODO implement
        holder.ivProfilePic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                picCallBack.notice(position);
            }
        });
        if(!TextUtils.isEmpty(object)){
            if (object.contains(HttpApi.BANNERUTL)){
                imageLoader.displayImage(object,holder.ivProfilePic);//HttpApi.BANNERUTL+
            }else {
                //imageLoader.displayImage(String.valueOf(Uri.fromFile(new File(object))), holder.ivProfilePic);
                imageLoader.displayImage("file://" + object, holder.ivProfilePic);
            }
        }

    }

    protected class ViewHolder {
        private ImageView ivProfilePic;
        public ViewHolder(View view) {
            ivProfilePic = (ImageView) view.findViewById(R.id.iv_profile_pic);
        }
    }
}
